//
//  DDQuestionBaseInfoViewController.m
//  dingdang
//
//  Created by Chen Jing on 15-7-18.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDQuestionBaseInfoViewController.h"
#import "DDNetWorkAPIClient.h"
#import "JSONKit.h"
#import "DDQuestionChooseCourseViewController.h"
#import "ModelQuestionCourse.h"
#import "AFHTTPRequestOperationManager.h"
#import "DDUserAgreementVC.h"

@interface DDQuestionBaseInfoViewController ()<DDQuestionChooseCourseViewControllerDelegate>{
    ModelQuestionCourseElement * param_course;
    NSString * param_title;
    NSString * param_password;
    NSString * param_numberOfRewardPerson;
    NSString * param_numberOfRewardEveryPerson;
    BOOL param_isOpenSwitch;
    NSMutableDictionary * paramDic;

}
@property (weak, nonatomic) IBOutlet UIButton *sendButton;

@property (weak, nonatomic) IBOutlet UIView *contentOfOption;
@property (weak, nonatomic) IBOutlet UILabel *label_title_rewardPeopleNumberOrSetSecretCode;
@property (weak, nonatomic) IBOutlet UILabel *label_choosedCourse;
@property (weak, nonatomic) IBOutlet UITextField *tf_setTitle;
@property (weak, nonatomic) IBOutlet UITextField *tf_setRewardPeopleNumberOrSecretCode;
@property (weak, nonatomic) IBOutlet UITextField *tf_setRewardForEveryPeople;
@property (weak, nonatomic) IBOutlet UISwitch *switch_isHideAnswerForAntherPeople;
@property (weak, nonatomic) IBOutlet UIView *contentOfOptionMain;
@property (weak, nonatomic) IBOutlet UILabel *label_tips;
@property (weak, nonatomic) IBOutlet UIView *contentOfFixHelp;
@property (weak, nonatomic) IBOutlet UISwitch *switch_receiveNoti;
@property (weak, nonatomic) IBOutlet UIButton *btn_agree;
@end

@implementation DDQuestionBaseInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)createUI{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    
    _sendButton.layer.cornerRadius = 5.0;
    self.switch_isHideAnswerForAntherPeople.hidden = NO;
    
    if (self.info_publishType == DDType_question_questionPublishType_Encryption) {
        self.contentOfOptionMain.frame = CGRectMake(0, 34, self.view.frame.size.width, 200 + 40);
        self.label_title_rewardPeopleNumberOrSetSecretCode.text = @"设置答案口令:";
        self.tf_setRewardPeopleNumberOrSecretCode.keyboardType = UIKeyboardTypeDefault;
        self.label_tips.hidden = NO;
        self.switch_isHideAnswerForAntherPeople.hidden = YES;
        [self addNavigatorTitle:NSLocalizedString(@"加密题", nil) parent:self.view];

    }else if (self.info_publishType == DDType_question_questionPublishType_Reward){
        [self addNavigatorTitle:NSLocalizedString(@"悬赏题", nil) parent:self.view];

    }else if (self.info_publishType == DDType_question_questionPublishType_help){
        self.contentOfOptionMain.frame = CGRectMake(0, 34, self.view.frame.size.width, 160);
        [self addNavigatorTitle:NSLocalizedString(@"大家帮", nil) parent:self.view];

    }

    //自定义TAB BAR UI
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];

    [self AddLeftBtnAction:@"" normal:@"top_返回" selected:@"top_返回" action:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];

    NSNumber * agreeNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"agree"];
    if (agreeNumber.boolValue == YES) {
        [self.btn_agree setImage:[UIImage imageNamed:@"checkBox_on"] forState:UIControlStateNormal];
    }else{
        [self.btn_agree setImage:[UIImage imageNamed:@"checkBox_off"] forState:UIControlStateNormal];
    }
}

-(void)createData{
    paramDic = [[NSMutableDictionary alloc]init];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)btn_publishClick:(id)sender {
    NSNumber * agreeNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"agree"];
    if (agreeNumber.boolValue == YES) {
        [self saveQuestionAndTopicSave];
    }else{
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"请点击同意协议后才能发布信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
}
- (IBAction)btn_setCourse:(id)sender {
    DDQuestionChooseCourseViewController * choose = [[DDQuestionChooseCourseViewController alloc]init];
    choose.info_publishType = self.info_publishType;
    choose.delegate = self;
    [self.navigationController pushViewController:choose animated:YES];
}

-(BOOL)assemblyDic{
    [paramDic removeAllObjects];

    [paramDic setObject:[NSNumber numberWithInteger:self.info_publishType] forKey:@"type"];



    [paramDic setObject:self.info_questions forKey:@"questions"];


    if (self.info_publishType == DDType_question_questionPublishType_Encryption) {

        param_password = self.tf_setRewardPeopleNumberOrSecretCode.text;
        if (param_password == nil || param_password.length < 1 ) {
            [self delayHUD:@"请填写密码"];
            return NO;
        }else{
            [paramDic setObject:param_password forKey:@"password"];
        }

        if (self.tf_setTitle.text.length > 0) {
            param_title = self.tf_setTitle.text;
        }

        if (param_course) {
            [paramDic setObject:param_course.info_id forKey:@"courseId"];
        }
        
        [paramDic setObject:[NSNumber numberWithBool:self.switch_receiveNoti.on] forKey:@"sendPush"];

        
        return YES;
    }else if (self.info_publishType == DDType_question_questionPublishType_help){
        if (self.tf_setTitle.text.length > 0) {
            param_title = self.tf_setTitle.text;
        }
        
        if (param_course) {
            [paramDic setObject:param_course.info_id forKey:@"courseId"];
        }
        
        [paramDic setObject:[NSNumber numberWithBool:self.switch_receiveNoti.on] forKey:@"sendPush"];
        
        return YES;
    }else if (self.info_publishType == DDType_question_questionPublishType_Reward){
        if (self.tf_setTitle.text.length > 0) {
            param_title = self.tf_setTitle.text;
        }

        if (param_course) {
            [paramDic setObject:param_course.info_id forKey:@"courseId"];
        }

        if (self.tf_setRewardPeopleNumberOrSecretCode.text.length > 0) {
            [paramDic setObject:self.tf_setRewardPeopleNumberOrSecretCode.text forKey:@"maxRewardNumber"];
        }else{
            [self delayHUD:@"请设置悬赏人数"];
            return NO;
        }

        if (self.tf_setRewardForEveryPeople.text.length > 0) {
            [paramDic setObject:self.tf_setRewardForEveryPeople.text forKey:@"reward"];
        }else{
            [self delayHUD:@"请设置悬赏积分/人"];
            return NO;
        }

        [paramDic setObject:[NSNumber numberWithBool:self.switch_receiveNoti.on] forKey:@"sendPush"];

        
        [paramDic setObject:[NSNumber numberWithBool:self.switch_isHideAnswerForAntherPeople.on] forKey:@"showAnswer"];

        return YES;
    }
    return NO;
}

-(void)saveQuestionAndTopicSave{
    
    BOOL canPublish = [self assemblyDic];
    if (canPublish == NO) {
        return;
    }
    DDNetWorkAPIClient * client = [[DDNetWorkAPIClient alloc]init];

    [client queryIntegral:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
        NSLog(@"%@",dict);
        if (isSuccess) {
          NSInteger  intgeral = [[dict objectForKey:@"integral"]integerValue];
          NSInteger  fillIntgeral = [[paramDic objectForKey:@"reward"] integerValue];
            
            NSInteger number = [[paramDic objectForKey:@"maxRewardNumber"] integerValue];
            if (number <= 0) {
                number = 1;
            }
            
            if (fillIntgeral*number<=intgeral) {
                [self showHUD:@"正在保存..."];
                [client mainTopicSave:paramDic completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
                    [self hideHUD];
                    
                    if ([message isEqualToString:@"成功"]) {
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"publishSuccess" object:nil];
                        [self.navigationController popViewControllerAnimated:YES];
                    }else{
                        [self delayHUD:message];
                    }
                }];
            }else{
                [self delayHUD:@"您的积分不足"];
            }
            
           
        }
    }];
    
    



}

-(void)choosedCourse:(ModelQuestionCourseElement *)element{
    param_course = element;
    dispatch_async(dispatch_get_main_queue(), ^{
        self.label_choosedCourse.text = element.info_title;
    });
}
- (IBAction)btnClick_agree:(id)sender {
    NSNumber * agreeNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"agree"];
    if (agreeNumber) {
        if (agreeNumber.boolValue == YES) {
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:NO] forKey:@"agree"];
        }else{
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"agree"];
        }
        [[NSUserDefaults standardUserDefaults]synchronize];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"agree"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    
    
    NSNumber * agreeNumber1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"agree"];

    if (agreeNumber1.boolValue == YES) {
        [self.btn_agree setImage:[UIImage imageNamed:@"checkBox_on"] forState:UIControlStateNormal];
    }else{
        [self.btn_agree setImage:[UIImage imageNamed:@"checkBox_off"] forState:UIControlStateNormal];
    }
}
- (IBAction)userProtocol:(id)sender {
    DDUserAgreementVC * vc = [[DDUserAgreementVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
